from django import forms
from file_manage import models
from django.shortcuts import render
from django.core.exceptions import ValidationError
# class UserForm(forms.Form):
#     username = forms.CharField(label="用户名", max_length=128)
#     password = forms.CharField(label="密码", max_length=256, widget=forms.PasswordInput)

class UserForm(forms.Form):
    username = forms.CharField(
        # label='用户名',
        min_length=1,
        max_length=20,
        widget=forms.TextInput(attrs={'class': 'input',
                                      'placeholder':"用户名",
                                        'type':"text",
                                      }
                               )
    )

    password = forms.CharField(
        # label='密  码',
        min_length=3,
        max_length=20,
        widget=forms.PasswordInput(attrs={'class': 'input',
                                          'placeholder':"密码",
                                          'type': "password",
                                          }),
    )

    # 对username字段进行检查后端验证
    def clean_username(self):
        username = self.cleaned_data['username']
        # try:
        models.User.objects.get(name=username)
        return username
        # except models.User.DoesNotExist: # 不检查存在多个结果的情况，注册(或者admin内)就不允许重复用户名
            # return False


class UploadForm(forms.Form):
    """上传多个文件"""
    files = forms.FileField(
        label='',
        widget=forms.ClearableFileInput(attrs={'multiple': True})
    )


class CreateDirectoryForm(forms.Form):
    """
        创建目录，和 mkdir view函数绑定
        todo: 添加具体的 clean 规则
        和 unix 系统的目录命名规则一致
    """
    name = forms.CharField(
        label='目录名 ',
        widget=forms.TextInput(attrs={'class': 'input'}),
    )

    def clean_name(self):
        name = self.cleaned_data.get('name')
        if '/' in name or '%' in name:
            raise ValidationError('!目录名不可以包含 "/" 或 "%"')
        else:
            return name.strip()


class EditForm(forms.Form):
    """
        编辑文件信息，
        todo 编辑文件信息
    """
    name = forms.CharField(
        label='文件名',
        widget=forms.TextInput(attrs = {'class': 'input'}),
    )
    def clean_name(self):
        name = self.cleaned_data.get('name')
        if '/' in name or '%' in name:
            raise ValidationError('抱歉，文件名不可以包含 "/" 或 "%"')
        else:
            return name.strip()

class ConfirmForm(forms.Form):
    """
        只是用于在重大操作之前进行提示的
    """
    confirm = forms.ChoiceField(
        label = '选择：',
        choices = (('y', '删除'), ('n', '按错了')),
        widget=forms.RadioSelect()
    )


class Confirm_Html_Form(forms.Form):
    #上传多个文件
    # file_s_yaml = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
    #将总用例需要跑的app位置
    confirm = forms.ChoiceField(
        label='选择：',
        choices=(('y', '确定运行此用例'), ('n', '按错了')),
        widget=forms.RadioSelect()
    )
    # app_choice=[("JULONG","聚龙")]
    app_choice=[]
    for tmp in models.App.objects.all():
        app_choice.append((tmp.app_path,tmp.name))
    search_T = forms.ChoiceField(choices=app_choice,
                                 label="app类型",
                                 widget=forms.Select(attrs={'onchange': "change_country(this.value)",
                                                            'class': "form-control",
                                                            'id': 'country',
                                                            }))